﻿using RockBus.DataContracts;
using System;
using System.ServiceModel.Channels;
using System.Threading.Tasks;

namespace RockBus.ServiceBus
{
    public class CorrelationContext
    {
        public CorrelationContext(RoutingHeader routingHeader, MessageVersion messageVersion)
        {
            this.RoutingHeader = routingHeader;
            this.MessageVersion = messageVersion;
        }

        public CorrelationContext(RoutingHeader routingHeader, MessageVersion messageVersion, TaskCompletionSource<Message> taskCompletionSource)
            : this(routingHeader, messageVersion)
        {
            this.TaskCompletionSource = taskCompletionSource;
        }

        public RoutingHeader RoutingHeader { get; private set; }

        public MessageVersion MessageVersion { get; private set; }

        public TaskCompletionSource<Message> TaskCompletionSource { get; private set; }
    }

    public class RequestResponseCorrelator
    {
        public RequestResponseCorrelator(CorrelationContext correlationContext, Action<object, CorrelationContext> responseCallback)
        {
            this.CorrelationContext = correlationContext;
            this.CorrelationResponseCallback = responseCallback;
        }

        private CorrelationContext CorrelationContext { get; set; }

        private Action<object, CorrelationContext> CorrelationResponseCallback { get; set; }

        public void ResponseCallback(object message)
        {
            this.CorrelationResponseCallback(message, this.CorrelationContext);
        }
    }
}